package cn.asterly.cicada;


import org.apache.log4j.Logger;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author likun
 * @Description  成成mybatis Mappert文件
 * @Date 10:37 2019-01-03
 **/
public class GennertorMapper {

    private static Logger logger = Logger.getRootLogger();
//    private final static Logger log= LoggerFactory.getLogger(CountServiceImpl.class);


    public static void main(String[] args) {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        Configuration config=null;
        //获取文件路径
        String path=GennertorMapper.class.
                getClassLoader().getResource("generatorConfig.xml").getPath();
        System.out.println(("文件路径："+path));

        File configFile = new File(path.substring(1));//直接放在文件下面，与pom.xml同级
        ConfigurationParser cp = new ConfigurationParser(warnings);
        try {
            //加载配置文件
            config= cp.parseConfiguration(configFile);
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);

            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
            myBatisGenerator.generate(null);
        } catch (IOException e) {
            logger.debug("配置文件加载异常");
        } catch (XMLParserException e) {
            logger.debug("配置文件解析异常");
        } catch (InvalidConfigurationException e) {
            logger.debug("执行异常");
        } catch (SQLException e) {
            logger.debug("sql异常");
        } catch (InterruptedException e) {
            logger.debug("终止异常");
        }
    logger.info("mybatis相关文件创建完成");
    }

}
